package com.weibo.zhifa.servlet;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import com.weibo.common.BaseSeverlet;
import com.weibo.zhifa.ZhiFa;

public class ZhifaServlet extends BaseSeverlet {
	private static Logger logger = Logger.getLogger(ZhifaServlet.class);

	@Override
	public void doService(HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("直发servelt被请求了！");
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
		ServletFileUpload servletFileUpload = new ServletFileUpload(
				diskFileItemFactory);

		ZhiFa zhifa = new ZhiFa();
		String starttime = "";
		String hour = "";
		String minute = "";
		String celue = "";
		String jiaji = "";
		String fangantime = "";
		String fangantype = "";
		HttpSession session = request.getSession();
		try {
			List<FileItem> fileItems = servletFileUpload.parseRequest(request);
			for (FileItem fileItem : fileItems) {
				if (fileItem.isFormField()) {
					String requestOfValue = fileItem.getString("UTF-8");
					String requestForName = fileItem.getFieldName();
					// System.out.println("表单中的普通参数: 上传人的姓名|文件等级 ("
					// + requestOfValue + "), 表单中的名字为'" + requestForName
					// + "'");
					if (requestForName.equals("pingtai")) {
						zhifa.setPla_id(requestOfValue);
					}
					if (requestForName.equals("task_type")) {
						zhifa.setTas_type(requestOfValue);
					}
					if (requestForName.equals("content_type")) {
						zhifa.setTas_cont_type(requestOfValue);
					}
					if (requestForName.equals("taskname")) {
						zhifa.setTas_name(requestOfValue);
					}
					if (requestForName.equals("starttime")) {
						starttime = requestOfValue;
					}
					if (requestForName.equals("hour")) {
						hour = requestOfValue;
					}
					if (requestForName.equals("minute")) {
						minute = requestOfValue;
					}
					if (requestForName.equals("straightContent")) {
						zhifa.setZhif_content(requestOfValue);
					}
					if (requestForName.equals("otherContent")) {
						zhifa.setTas_other(requestOfValue);
					}
					if (requestForName.equals("celue")) {
						celue = requestOfValue;
					}
					if (requestForName.equals("jiaji")) {
						jiaji = requestOfValue;
					}
					if (requestForName.equals("fangantime")) {
						fangantime = requestOfValue;
					}
					if (requestForName.equals("fangantype")) {
						fangantype = requestOfValue;
					}

				} else {
					// 获得上传文件的名字 (文件上传框中, 上传文件的名字, 注意: 该名字已被截取, 并不是文件的完整路径)
					String fileName = fileItem.getName();
					if (fileName.contains("\\")) {
						fileName = fileName.substring(
								fileName.lastIndexOf("\\") + 1,
								fileName.length());
					}
					// 用户选择了上传的文件, 并且该文件的名字不为空
					if (fileItem.getName() != null
							&& !"".equals(fileItem.getName())) {
						// 获得上传文件的类型
						String contentType = fileItem.getContentType();

						// 获得上传文件的大小
						long fileSize = fileItem.getSize();

						// 打印Http请求消息 ('Request') 中上传文件的信息
						System.out.println("上传的文件名: " + fileName);
						System.out.println("上传的文件类型: " + contentType);
						System.out.println("上传的文件大小: " + fileSize);

						// 获得项目的根目录
						String rootDirProject = getServletContext()
								.getRealPath("/");

						// 拼接保存的上传文件的目录
						File saveFileUploadFile = new File(rootDirProject
								+ "upload");

						// 如果保存上传文件的目录不存在, 创建
						if (!saveFileUploadFile.exists()) {
							saveFileUploadFile.mkdirs();
						}

						// 拼接保存上传内容的文件
						File saveTheFileUpload = new File(saveFileUploadFile,
								fileName);
						// 保存上传的文件
						fileItem.write(saveTheFileUpload);
						if (contentType.indexOf("image") >= 0) {
							zhifa.setZhif_img(request.getScheme() + "://"
									+ request.getServerName() + ":"
									+ request.getServerPort()
									+ getServletContext().getContextPath()
									+ "/upload/" + fileName);
						}
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		zhifa.setTas_start_time(starttime + " " + hour + ":" + minute);
		session.setAttribute("zhifa", zhifa);
		request.setAttribute("celue", celue);
		request.setAttribute("jiaji", jiaji);
		request.setAttribute("fangantype", fangantype);
		try {
			request.getRequestDispatcher(
					"/WEB-INF/task/Task_add_straight_confirm.jsp").forward(
					request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
